<?php

/**
 * Controller_teaching
 * 学生评语。，学生日常记事
 */

class Controller_message extends MyApp {

	public function __construct()
	{

		parent::__construct();
        $this->model->file  = YOU::loadModel('file');
		$this->model->class    = YOU::loadModel('class');
		$this->model->divide   = YOU::loadModel('divide');
		$this->model->student  = YOU::loadModel('student');
		$this->model->message  = YOU::loadModel('message');
		$this->model->semester = YOU::loadModel('semester');
		$this->model->log 	   = YOU::loadModel('log');

	}
	/** 首页 **/
	public function actionIndex()
	{

	}

	/** -----------------------------学生评语和学生日常记事--------------------------------- **/
	/** 学生期末评语学生列表 **/
	public function actionStudentList ()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);

		// 老师有哪些班级
		$appoint_lists = $this->db->fetchAll("SELECT * FROM `tbl_appoint` WHERE staff_id='{$this->user['type_id']}' AND appoint_type = '4'");
		// 获得班级的名称
		foreach ($appoint_lists as $key => $value){
			// 班级名称
			$class_data = $this->model->class->getinfo($value['class_id']);
			$appoint_lists[$key]['class_name'] = $class_data['class_name'];
		}
		// 获得班级的id
		$class_id   = (int)$this->get->class_id;
		if(empty($class_id)){
			foreach ($appoint_lists as $key => $value){
				$class_id = $appoint_lists[0]['class_id'];
			}
		}

		// 获取学期id
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		// 进入学生评语或者是日常记事的通道
		$message_type = $this->get->message_type;
		// 页面名称
		$show_name = $this->show_name[$message_type];
		// 页面名称
		$show_content = $this->show_content[$message_type];
		// 展列出这个班级的所有学生
		$divide_lists= $this->model->student->getClassAll($class_id);

		foreach ($divide_lists as $key => $value) {

			// 学生名称，学号
			$student_data = $this->model->student->getinfo($value['student_id']);
			$divide_lists[$key]['student_no']  =$student_data['student_no'];
			$divide_lists[$key]['student_name']= $student_data['student_name'];
			
			// 评语内容
			$message_data = $this->model->message->getRow($value['student_id'], $message_type,$semester_id);
			
			$divide_lists[$key]['message_file'] 	= $message_data['message_file'];
			$divide_lists[$key]['message_content'] 	= $message_data['message_content'];
			$divide_lists[$key]['message_id'] 	   	= $message_data['message_id'];

		}
		
		if ($_POST) {
			//var_dump($_POST);
			//var_dump($_FILE);exit;
			
			
			
			
			$student_id 	  	  = $this->post->student_id;
			$message_content 	  = $this->post->message_content;
			$message_id 	      = $this->post->message_id;
			$message_file 	      = $this->post->message_file;

			$message_datas = $this->model->message->getRow($student_id, $message_type,$semester_id);

			if (!empty($message_datas)) {

				$info = array(

				"message_content"  		=> $message_content,
				//"message_file"  		=> $message_file,
				);

				$this->db->do_update("tbl_message", $info, "message_id='{$message_id}'");

                // 附件上传
                $file = $this->model->file->upload("message_file", $message_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));
                if (!empty($file)){
                    $info = array(
                        "message_file" => $file['fname'],
                    );

                    $this->db->do_update("tbl_message", $info, "message_id='{$message_id}'");
                }

				// 跳转
				Helper_Client::location("修改成功", "index.php?a=message.studentlist&message_type={$message_type}&class_id={$class_id}");
			}else{

				$info = array(

				"student_id"  			=> $student_id,
				"message_content"  		=> $message_content,
				"semester_id"  			=> $semester_id,
				"message_type"  		=> $message_type,
				"user_id"  				=> $this->user['user_id'],
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);
                $message_id = $this->db->do_insert("tbl_message", $info);
                // 附件上传
                $file = $this->model->file->upload("message_file", $message_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));
                if (!empty($file)){
                    $info = array(
                        "message_file" => $file['fname'],
                    );

                    $this->db->do_update("tbl_message", $info, "message_id='{$message_id}'");
                }

				// 跳转
				Helper_Client::location("添加成功", "index.php?a=message.studentlist&message_type={$message_type}&class_id={$class_id}");
			}

		}

		$this->tpl->assign("divide_lists", $divide_lists);
		$this->tpl->assign("show_name", $show_name);
		$this->tpl->assign("show_content", $show_content);
		$this->tpl->assign("appoint_lists", $appoint_lists);
		
		$this->tpl->assign("class_id", $class_id);
		$this->tpl->assign("message_type", $message_type);
		$this->tpl->display('message/class.list.tpl.php');
	}
	/** ---------------------------------往年学生评语------------------------------------- **/
	/** 往年学年评语列表 **/
	public function actionSemesterList ()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		

		// 老师有哪些班级
		$appoint_lists = $this->db->fetchAll("SELECT * FROM `tbl_appoint` WHERE staff_id='{$this->user['type_id']}' AND appoint_type = '4'");

		foreach($appoint_lists as $key => $value){
			// 班级名称
			$class_name= $this->model->class->getInfo($value['class_id']);
			$appoint_lists[$key]['class_name'] = $class_name['class_name'];
		}

		// 进入学生评语或者是日常记事的通道
		$message_type = $this->get->message_type;
		// 获取所有的学期
		$semester_lists= $this->model->semester->getAll();
		// 搜索
		$semester_id 	      = $this->get->semester_id;
		$class_id	 	      = $this->get->class_id;

		$where = " ";

		if(!empty($semester_id)){

			$where .= " AND a.semester_id ='{$semester_id}' ";
			$this->tpl->assign("semester_id", $semester_id);

		}
		if(!empty($class_id)){
			$where .= " AND b.class_id ='{$class_id}' ";

			$this->tpl->assign("class_id", $class_id);
		}

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_message` AS a,`tbl_student` AS b WHERE a.student_id=b.student_id AND a.message_type='{$message_type}' AND a.user_id ='{$this->user['user_id']}' {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		// 有学期id值时才会有数据
		if (!empty($semester_id)) {

			$divide_lists = $this->db->fetchAll("SELECT * FROM `tbl_message` AS a,`tbl_student` AS b WHERE a.student_id=b.student_id AND a.message_type='{$message_type}' AND a.user_id ='{$this->user['user_id']}' {$where} ORDER BY b.student_no DESC LIMIT {$start}, ".PAGE_LIMIT."");
			foreach ($divide_lists as $key => $value) {

				// 学生名称，学号
				$student_data = $this->model->student->getinfo($value['student_id']);
				$divide_lists[$key]['student_no']  =$student_data['student_no'];
				$divide_lists[$key]['student_name']= $student_data['student_name'];
				// 评语内容
				$message_data = $this->model->message->getRow($value['student_id'], $message_type,$semester_id);
				// 截取字符串长度
				$message_data['message_content'] = mb_substr($message_data['message_content'],0,27,'utf-8');
				if(strlen($message_data['message_content'])>= 27){

					$message_data['message_content'] = $message_data['message_content'].'...';
				}
				$divide_lists[$key]['message_content'] = $message_data['message_content'];
				$divide_lists[$key]['message_id'] 	   = $message_data['message_id'];

			}
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("divide_lists", $divide_lists);

		$this->tpl->assign("appoint_lists", $appoint_lists);
		$this->tpl->assign("message_type", $message_type);
		$this->tpl->assign("semester_lists", $semester_lists);
		$this->tpl->display('message/semester.list.tpl.php');
	}
	// 往年学期评语 ->详情
	public function actionDetails()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$student_id 	      	  = $this->get->student_id;
		$message_id 	          = $this->get->message_id;
		// 获取学生姓名 学号
		$student_data = $this->model->student->getInfo($student_id);
		// 获取内容
		$message_data = $this->model->message->getinfo($message_id);

		$this->tpl->assign("student_data", $student_data);
		$this->tpl->assign("message_data", $message_data);
		$this->tpl->display('message/semester.details.tpl.php');
	}
	/** 下载**/
	public function actionoutput(){
		$fname = $this->get->manage_file;
		$fpath = UPLOAD_PATH."/manage_file/{$fname}";

		// 输出下载文件头s
		header("Content-Disposition: attachment; filename={$fname}");
		// 在线输出
		readfile($fpath);

	}
}

?>